﻿-- Author      : Kamil
-- Create Date : 9/20/2012 7:18:01 PM
local lastMsg = 0;
local newMsg = 0;
local chatLine = 0;
local lastKill = 0;
local lastDeath = 0;
local lastChannelJoin = 0;
local lastAddonLoad = 0;
local lastBGresult = 0;

targetList = {};



function AddonMessageHandler(prefix, message, distribution, sender, other1, other2, other3)
	
	if (strsub(prefix,0,4) == CONST) then
		--print('recived: ' .. prefix .. ' : ' .. message .. ' - ' .. distribution .. ' (' .. sender .. ')' );
		
		local fod = string.find(message, '{');
		
		newMsg = message;
		message = strsub(message,0,fod-1);
		
		
		if not (lastMsg == newMsg) then
		
			-- link profesji
			if (prefix == PX_SHOW_PROFESSIONS) then ShowProfessions(message, sender, distribution); end
			
			-- pokazanie latency
			if (prefix == PX_SHOW_LATENCY) then ShowMyLatency(distribution); end
			
			-- ping do guild memberow, przed invitem
			if (prefix == PX_PING_GUILD) then RespondToPing(message, sender); end
			
			-- odpowiedz na ping
			if (prefix == PX_RESPOND_TO_PING) then InvitePlayer(sender); end
			
			-- pokazanie statystyk
			if (prefix == PX_SHOW_STATISTICS) then ShowMyStatistics(); end
			
			-- guild warning
			if (prefix == PX_GUILD_WARNING) then SetGuildWarningText(message,sender); end
			
			--wg time requested
			if (prefix == PX_WG_REQUEST) then respondWGTime(); end
			
			--wg time respond
			if (prefix == PX_WG_RESPOND) then NextWGTime = message;  end
			
			--get rat
			if (prefix == PX_GET_RATTING) then ShowMyBGRatting();  end
		end	
		
		lastMsg = newMsg;
	end
end

function ShowMyBGRatting()
	local playedBG = GetStatistic(839);
	local wonBG = GetStatistic(840);
	if (playedBG == "--") then playedBG = 0; end;
	if (wonBG == "--") then wonBG = 0; end;	
	local losBG = playedBG - wonBG; 
	SendChatMessage("BG Ratting: "..bgRatting[UnitName('player')].ratting.." - ("..wonBG.." bg Win & "..losBG.." bg Loose)","GUILD");
end

function PartyInviteEvent(sender)
	if (InviteAutoAccept) then AcceptGroup(); end
	InviteAutoAccept = false;
end

function GuildMessageEvent(message, sender, line, senderGUID)
	
	if not(chatLine == line) then
		if (message == 'show versions') then SendChatMessage(VERSION,"GUILD"); end
		chatLine = line;
	end
end

function TargetChangedEvent()
	local level = UnitLevel("target");
	local guild;
	local class = UnitClass("target");
	local myLvl = UnitLevel("player");
	local triv = false;
	
	if (myLvl - level > 9) then triv = true; end;
	
	if ((UnitIsEnemy("target","player")) and not ("target"==nil) and UnitIsPlayer("target"))  then
		
		 if not (GetGuildInfo("target")==nil) then guild = '<' .. GetGuildInfo("target") .. '>' else guild="<???>"; end
		
		if (targetList[UnitName("target")] == nil) then 
			targetList[UnitName("target")] = targets(level,guild,class, triv);
			
			
		end
	end
end

function CombatLogEventHandler(tStamp, subEvent, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellID, spellName, spellSchool)
	
	if ((subEvent == "PARTY_KILL") and (sourceGUID == UnitGUID("player")) and not (lastKill == tStamp)) 
	then
			locClass, engClass, locRace, engRace, gender = GetPlayerInfoByGUID(destGUID);
			local zone = GetZoneText();
			
			if not (locClass == nil) then
				NoticePartyKill(destName, locClass, locRace, zone, gender) ;
				lastKill = tStamp;
			end
	end	
	
end

function PlayerKilledEvent(timeStamp)
	if not (lastDeath == timeStamp) then
		SetKillMsgFrame('Padłes z wynikiem: ' .. kWithNoDeath .. ' kili ;f', kWithNoDeath);
		kWithNoDeath = 0;
		lastDeath = timeStamp;
	end
end;

function PlayerJoinedChannelEvent(timeStamp, player)
	if not (lastChannelJoin == timeStamp) then
		DEFAULT_CHAT_FRAME:AddMessage("|cff773377[" .. player .. "] się narysował.");
		lastChannelJoin = timeStamp;
	end
end

function MerchantShowEvent()
		qstr="";
		i=0;
		repeat
			if not(GetContainerNumSlots(i)==nil)then
				caslots=GetContainerNumSlots(i);

				j=1;
				repeat
					calink=GetContainerItemLink(i,j);
					if not(calink==nil)then                        
						_,_,caquality,_,_,_,_,_,_,_=GetItemInfo(calink);
						if(caquality==0)then
							UseContainerItem(i,j);
						end
					end
					j=j+1;
				until j>=caslots+1;
			end
			i=i+1;
		until i>=5
end;

function PlayerLVLedEvent(lvl)
	SendChatMessage(" >> LVL UP! ("..lvl..") <<","GUILD");
    StopMusic(); PlaySoundFile("Interface\\AddOns\\mnk\\Sound\\up.mp3"); 
end

function AddonLoadedEvent(addonName, loadTime)
	if ((addonName == 'mnk') and not (lastAddonLoad == loadTime)) then
		lastAddonLoad = loadTime;
		
		
	
		if (btnConfiguration[0] == nil) then
			btnConfiguration[0] = ButtonConfigurations(1, 1, 1, 1, 1, 1, 1);
		end
		
		if (panelConfiguration[0] == nil) then
			panelConfiguration[0] = panelConfigurations(3,2,1);
		end
		
		if (frmConfiguration[0] == nil) then
			frmConfiguration[0] = frameConfigurations(-380,-5);
		end
		
		if (bgRatting[UnitName('player')] == nil) then
			bgRatting[UnitName('player')] = bgRattings(1000,0,0,0,0);
		end
		
		
		killInfoLocation:SetValueStep(1);
		killInfoLocation2:SetValueStep(1);
		panelScale:SetValueStep(5);
		killInfoLocation:SetValue(frmConfiguration[0].killInfo_x);
		killInfoLocation2:SetValue(frmConfiguration[0].killInfo_y);
		panelScale:SetValue(panelConfiguration[0].pnScale*100);
		
		setFrames();
		setCheckBoxes();
		redrawPanel();
		
		setRattingLabels();
	end
end

function bgResultEvent(txt,tmt)
	if not (lastBGresult == tmt) then
		if (txt == "The Alliance wins!") then bgRattingDown(); end;
		if (txt == "The Horde wins!") then bgRattingUp(); end;
		lastBGresult = tmt;
	end
end